//主背景
var HeroPlayer = require("player");
var MoveBg = require("moveBg");
cc.Class({
    extends: cc.Component,

    properties: {
		//用于获取主教弹跳高度 控制主角行动开关
		player:{
			default:null,
			type:cc.Node
		},
		//背景移动
		bgsprite1:{
			default:null,
			type:cc.Node
		},
		bgsprite2:{
			default:null,
			type:cc.Node
		},
		//分数
		scoreDisplay:{
			default:null,
			type:cc.Node
		}
    },
	//事件监听
    setEventControl: function(){
        var self = this;
        var hero = self.player.getComponent(HeroPlayer);//角色绑定控件
		var bg1 = self.bgsprite1.getComponent(MoveBg);
		var bg2 = self.bgsprite2.getComponent(MoveBg);
		cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
              // 设置是否吞没事件，在 onTouchBegan 方法返回 true 时吞没
            onTouchBegan: function (touch, event) {
                 //实现 onTouchBegan 事件回调函数
                var target = event.getCurrentTarget();
                // 获取事件所绑定的 target
				var locationInNode = target.convertToNodeSpace(touch.getLocation());
				cc.log("当前点击坐标"+locationInNode);
                hero.node.runAction(hero.setJumpUpAction());//精灵移动
                //cc.log("跳跃：－－－－－－－－");
                return true;
            },
			// 触摸移动时触发
            onTouchMoved: function (touch, event) {            
            },
			// 点击事件结束处理
            onTouchEnded: function (touch, event) {    
				if(self.player.getPositionY() > 0){
					var height = self.player.getPositionY();
					self.player.setPositionY(height/2);
					self.gainScore();
					
					bg1.node.runAction(bg1.setMoveAction(height));//背景实现向下滚动
                    bg2.node.runAction(bg2.setMoveAction(height));//背景实现向下滚动

				}
                //  cc.log("跳跃后角色坐标：" + self.player.getPosition() );
            }
        }, self.node)
    },
	setBgMoveCreate: function(){
        //如果背景1的坐标移出屏幕开始设置新的坐标
        if(this.bgsprite1.getPositionY()  < -500 ){
            this.bgsprite2.setPositionY(this.bgsprite1.getPositionY()+this.bgsprite1.getContentSize().height);
        }
        //如果背景2的坐标移出屏幕开始设置新的坐标
        if(this.bgsprite2.getPositionY() < -500 ){
            this.bgsprite1.setPositionY(this.bgsprite2.getPositionY()+this.bgsprite2.getContentSize().height);
        }
	},
	gameOver: function () {
        cc.eventManager.removeAllListeners();//移除所有事件监听
        this.player.stopAllActions(); //停止 player 节点的跳跃动作
        cc.director.loadScene("GameOver");//切换场景到结束场景
    },
	onLoad: function () {
        //触摸监听
        this.setEventControl();
        // 初始化计分
        this.score = 0;
		
		this.isMoving = true;
    },
	//实现update方法
	update: function (dt) {
		this.setBgMoveCreate();//检测背景
		         
		//gameOver判断 玩家坠落到屏幕底部游戏结束
		if(this.player.getPositionY() <= -cc.view.getVisibleSize().height/2){
			this.unscheduleAllCallbacks();
			if(this.isMoving){
				this.gameOver();
				this.isMoving = false;
			}
		}
	},
	//积分更新
    gainScore: function () {
        this.score += 1;
        // 更新 scoreDisplay Label 的文字
        this.scoreDisplay.string =  this.score.toString();
        cc.sys.localStorage.setItem("ScoreDis" ,this.scoreDisplay.string);//本地存储
    },
});
